package com.crc.luojian.resolver.impl;

import com.crc.luojian.annotation.RequestParam;
import com.crc.luojian.resolver.RequestResolver;
import com.crc.luojian.util.RequestParameterConverter;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Parameter;

/**
 * @program: luojian-springframework
 * @description:
 * @author: luojian58
 * @create: 2021-06-11 10:13
 */
public class RequestParamResolver implements RequestResolver {
    @Override
    public boolean support(Parameter parameter) {
        return parameter.getAnnotation(RequestParam.class) != null;
    }

    @Override
    public Object resolveArgument(HttpServletRequest request,Parameter parameter) {
        RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
        String value = request.getParameter(requestParam.value());
        RequestParameterConverter converter = new RequestParameterConverter();
        return converter.convert(parameter.getType(), value);
    }
}
